;(function($){
	//--------------------- public methods --------------------------
    function pp(info){
       if (window.console && window.console.log)    
            window.console.log('LOG: ' + info ); 
    };	
	
	function creat_indicators(obj,opts){	
		obj.append("<div class='hd'><ul></ul></div>");
	};
	
	function dispose_className(ele, className){
		if( !ele || !className || (ele.className && ele.className.search(new RegExp("\\b" + className + "\\b")) != -1) ) 
			return;
	}
	
	$.fn.lt_slide = function(options){
		// 将defaults 和 options 参数合并到{}   
		var opts = $.extend({}, $.fn.lt_slide.defaults, options);  			
	    return this.each(function() {
			obj = $(this)
			//创建indicators
			creat_indicators(obj,opts);			
			var opts = {
				titCell:options.titCell || ".hd li", // 当自动分页设为true时为“导航对象包裹层”
				mainCell:options.mainCell || ".slide", // 切换对象包裹层
				effect:options.effect || "left", // 效果，支持 left、leftLoop
				autoPlay:options.autoPlay || false, // 自动播放
				delayTime:options.delayTime || 800, // 效果持续时间
				interTime:options.interTime || 2000, // 自动运行间隔
				defaultIndex:options.defaultIndex || 0, // 索引
				titOnClassName:options.titOnClassName ||"on", // 给滚动的点添加className
				autoPage:options.autoPage || false, // 滚动的点
				pageStateCell:options.pageStateCell ||".pageState", //显示分页状态，例如：2/3
				switchLoad: null //切换图片
			}
			var obj = function(str,parEle){ 
				str = str.split(" ");
				var par = [];
				parEle = parEle||document;
				var retn = [ parEle ] ;
				for( var i in str ){ 
					if(str[i].length!=0) par.push(str[i]) 
				}
				for( var i in par ){ 
					if( retn.length==0 ) return false;
					var _retn = [];
					for ( var r in retn )
					{
						if( par[i][0] =="#" ) _retn.push( document.getElementById( par[i].replace("#","") ) );
						else if( par[i][0] =="." ){
							var tag = retn[r].getElementsByTagName('*');
							for( var j=0; j<tag.length; j++ ){
								var cln = tag[j].className;
								if( cln && cln.search(new RegExp("\\b" + par[i].replace(".","") + "\\b"))!=-1 ){ _retn.push( tag[j] ); }
							}
						}
						else { 
							var tag = retn[r].getElementsByTagName( par[i] ); 
							for( var j=0; j<tag.length; j++ ){ 
								_retn.push( tag[j] ) 
							} 
						}
					}
					retn =_retn;
				}				
				return retn.length==0 || retn[0] == parEle ? false:retn;
			}
			var wrap = function(el, v){
					var tmp = document.createElement('div');
					tmp.innerHTML = v;
					tmp = tmp.children[0];
					var _el = el.cloneNode(true);
					tmp.appendChild(_el);
					el.parentNode.replaceChild(tmp, el);
					conBox = _el;
					return tmp;
				};
			
			// className处理			
			var addClass =function(ele, className){
					dispose_className(ele, className);
					ele.className += (ele.className ? " " : "") + className;
				}
			var removeClass = function(ele, className){
					dispose_className(ele, className);
					ele.className = ele.className.replace(new RegExp("\\s*\\b" + className + "\\b", "g"), "");
				}
				
			var effect = opts.effect;
			var pageState = obj( opts.pageStateCell )[0];
			var conBox = obj( opts.mainCell)[0];
			if( !conBox ) 
			return false;
			var conBoxSize= conBox.children.length;
			var navObj = obj( opts.titCell );
			var navObjSize = navObj?navObj.length:conBoxSize;
			var sLoad=opts.switchLoad;
			var index=parseInt(opts.defaultIndex);
			var delayTime=parseInt(opts.delayTime);
			var interTime=parseInt(opts.interTime);
			var autoPlay = (opts.autoPlay=="false"||opts.autoPlay==false)?false:true;
			var autoPage = (opts.autoPage=="false"||opts.autoPage==false)?false:true;
			var loop = (opts.pnLoop=="false"||opts.pnLoop==false)?false:true;
			var oldIndex = index;
			var inter=null;
			var timeout = null;
			var endTimeout = null;		
			var startX = 0;
			var startY = 0;
			var distX = 0;
			var distY = 0;
			var dist = 0;
			var isTouchPad = (/hp-tablet/gi).test(navigator.appVersion);
			var hasTouch = 'ontouchstart' in window && !isTouchPad;
			var touchStart = hasTouch ? 'touchstart' : 'mousedown';
			var touchMove = hasTouch ? 'touchmove' : '';
			var touchEnd = hasTouch ? 'touchend' : 'mouseup';
			var slideH=0;
			var slideW= conBox.parentNode.clientWidth;
			var twCell;
			var scrollY ;
			var tempSize = conBoxSize;			
			//处理分页
			if( navObjSize==0 )
				navObjSize=conBoxSize;
			if( autoPage ){
				navObjSize=conBoxSize;
				navObj=navObj[0];
				navObj.innerHTML=""; 
				var str="";
				if( opts.autoPage==true|| opts.autoPage=="true" ){ 
					for( var i=0; i<navObjSize; i++ ){ 
						str+="<li>"+(i+1)+"</li>" 
					} 
				}
				else{ 
					for( var i=0; i<navObjSize; i++ ){ 
						str+=opts.autoPage.replace("$",(i+1))  
					} 
				}				
				navObj.innerHTML=str;
				navObj = navObj.children;
			}			
			if( effect == "leftLoop" ){
				tempSize +=2;
				conBox.appendChild( conBox.children[0].cloneNode(true) );
				conBox.insertBefore( conBox.children[conBoxSize-1].cloneNode(true),conBox.children[0] );
			}
			twCell = wrap(conBox,'<div class="tempWrap" style="overflow:hidden; position:relative;"></div>');
			conBox.style.cssText="width:"+tempSize*slideW+"px;"+"position:relative;overflow:hidden;padding:0;margin:0;";
			for ( var i =0; i<tempSize; i++ ){  
				conBox.children[i].style.cssText="display:table-cell;vertical-align:top;width:"+slideW+"px"  
			}			
			var doSwitchLoad=function( moving ){ 
				var curIndex = ( effect=="leftLoop"?index+1:index ) + moving;
				var changeImg = function( ind ){
						var img = conBox.children[ind].getElementsByTagName("img");
						for ( var i=0; i<img.length ; i++ ){
							if ( img[i].getAttribute(sLoad) ){ 
								img[i].setAttribute("src", img[i].getAttribute(sLoad) ); 
								img[i].removeAttribute( sLoad );
							} 
						}
					}
				changeImg( curIndex );
				if( effect=="leftLoop" ){
					switch ( curIndex ){
						case 0: changeImg( conBoxSize );break;
						case 1: changeImg( conBoxSize+1 );break;
						case conBoxSize: changeImg( 0 );break;
						case conBoxSize+1: changeImg( 1 );break;
					}
				}
			}
			
			//滑动效果
			var translate = function( dist, speed, ele ) {
				if( !!ele ){ 
					ele=ele.style; 
				}
				else{ 
					ele=conBox.style; 
				}
				ele.webkitTransitionDuration 
				=  ele.MozTransitionDuration 
				= ele.msTransitionDuration 
				= ele.OTransitionDuration 
				= ele.transitionDuration
				= speed + 'ms';
				ele.webkitTransform = 'translate(' + dist + 'px,0)' + 'translateZ(0)';
				ele.msTransform 
				= ele.MozTransform 
				= ele.OTransform 
				= 'translateX(' + dist + 'px)';		
			}
			
			//创建效果
			var doPlay=function(isTouch){				
				switch (effect){
					case "left": 
						if ( index >= navObjSize ) { 
							index = isTouch?index-1:0; 
						} 
						else if( index < 0 ) { 
							index = isTouch?0:navObjSize-1; 
						} 
						if( sLoad!=null ){ 
							doSwitchLoad(0) 
						} 
						translate( (-index*slideW),delayTime ); 
						oldIndex=index;
					break;							
					case "leftLoop":
						if( sLoad!=null ){ doSwitchLoad(0) }
						translate(  -(index+1)*slideW ,delayTime );
						if ( index==-1 ){ 
								timeout= setTimeout( function(){ 
									translate( -navObjSize*slideW ,0 ); 
								}, delayTime );
								index = navObjSize-1; 
						}
						else if( index==navObjSize ){ 
							timeout= setTimeout( function(){ 
								translate( -slideW ,0 ); 
							}, delayTime );
							index = 0; 
						}
						oldIndex=index;
					break;
				}
				//设置className
				for ( var i=0; i<navObjSize; i++ ){
					removeClass(navObj[i],opts.titOnClassName);
					if( i == index ){ 
						addClass(navObj[i],opts.titOnClassName) 
					}
				}				
				//设置1/3
				if(pageState){ 
					pageState.innerHTML= "<span>"+(index+1)+"</span>"+navObjSize; 
				}
			};
			doPlay();			
			
			//自动播放
			if (autoPlay) {
				 inter=setInterval(function(){ 
					index++; 
					doPlay();
				}, interTime); 
			}
			
			//触摸效果
			var tStart = function(e){
				clearTimeout( timeout );
				scrollY = undefined;
				distX = 0;
				var point = hasTouch ? e.touches[0] : e;
				startX =  point.pageX;
				startY =  point.pageY;
				conBox.	addEventListener(touchMove, tMove,false);
				conBox.addEventListener(touchEnd, tEnd ,false);
			}			
			
			//触摸移动函数			
			var tMove = function(e){
				if( hasTouch ){ 
					if ( e.touches.length > 1 || e.scale && e.scale !== 1) return 
				};
				var point = hasTouch ? e.touches[0] : e;
				distX = point.pageX-startX;
				distY = point.pageY-startY;
				if ( typeof scrollY == 'undefined') { scrollY = !!( scrollY || Math.abs(distX) < Math.abs(distY) ); }
				if( !scrollY ){ 
					e.preventDefault(); 
					if(autoPlay){
						clearInterval(inter) 
					}
					switch (effect){
						case "left":
							if( (index==0 && distX>0) || (index>=navObjSize-1&&distX<0 )){ distX=distX*0.4 }
							translate( -index*slideW+distX ,0 );
						break;
						case "leftLoop":translate( -(index+1)*slideW+distX ,0 );break;
					}					
					if(  sLoad!=null && Math.abs(distX)>slideW/3 ){ 
						doSwitchLoad( distX>-0?-1:1 ) 
					}
				}
			}		
			var tEnd = function(e){
				if(distX==0) return;
				e.preventDefault(); 
				if( !scrollY )
				{
					if( Math.abs(distX) > 300 ){ 		
						distX>0? index--: index++; 
					}					
					doPlay( true );  
					if (autoPlay) {
						 inter=setInterval(function(){ index++; doPlay() }, interTime); 
					}
				}
				conBox.removeEventListener(touchMove, tMove, false);
				conBox.removeEventListener(touchEnd, tEnd, false);
			}			
			conBox.addEventListener(touchStart, tStart ,false);			
		});
	};	
})(Zepto);
